package com.ss.android.common.app.permission;

import android.Manifest;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.util.Log;

import com.ss.android.common.dialog.AlertDialog;

import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import inline.PermissionUtils;

public class PermissionsManager {
    private static final String TAG = PermissionsManager.class.getSimpleName();
    private static a b;
    private static PermissionsManager sInstance;
    private static Map h;

    static {
        sInstance = null;
        h = new HashMap();
        h.put("android.permission.ACCESS_COARSE_LOCATION", Integer.valueOf(com.ss.android.article.browser.R.string.REQUEST_PERMISSION_DESCRIPT_LOCATION));
        h.put("android.permission.READ_EXTERNAL_STORAGE", Integer.valueOf(com.ss.android.article.browser.R.string.REQUEST_PERMISSION_DESCRIPT_EXTERNAL_STORAGE));
        h.put("android.permission.WRITE_EXTERNAL_STORAGE", Integer.valueOf(com.ss.android.article.browser.R.string.REQUEST_PERMISSION_DESCRIPT_EXTERNAL_STORAGE));
        h.put("android.permission.READ_SMS", Integer.valueOf(com.ss.android.article.browser.R.string.REQUEST_PERMISSION_DESCRIPT_SMS));
        h.put("android.permission.READ_CONTACTS", Integer.valueOf(com.ss.android.article.browser.R.string.REQUEST_PERMISSION_DESCRIPT_CONTACT));
        h.put("android.permission.CAMERA", Integer.valueOf(com.ss.android.article.browser.R.string.REQUEST_PERMISSION_DESCRIPT_CAMERA));
    }

    private final Set<String> mRequestingPermissions;
    private final Set d;
    private final List<WeakReference<PermissionsResultAction>> e;
    private final List f;

    private PermissionsManager() {
        this.mRequestingPermissions = new HashSet(1);
        this.d = new HashSet(1);
        this.e = new ArrayList(1);
        this.f = new ArrayList(1);
        this.b();
    }

    public static PermissionsManager getInstance() {
        if (sInstance == null) {
            sInstance = new PermissionsManager();
        }
        return sInstance;
    }

    public static void a(a arg0) {
        b = arg0;
    }

    public final void notifyPermissionsChange(@NonNull Activity activity, @NonNull String[] arg10, @NonNull int[] arg11, boolean arg12) {
        Iterator<WeakReference<PermissionsResultAction>> v4_2;
        int v0_3;
        String v0_2 = null;
        int v2_2;
        StringBuilder v5;
        String v4_1;
        boolean v2_1;
        String v2;
        int v3;
        CharSequence v0 = null;
        if (!arg12) {
        } else {
            try {
                int v4 = arg10.length;
                v3 = 0;
                while (true) {
                    label_6:
                    if (v3 >= v4) {
                        break;
                    }

                    v2 = arg10[v3];
                    if (arg11[v3] == -1) {
                        if (Build.VERSION.SDK_INT >= 23) {
                            v2_1 = activity.shouldShowRequestPermissionRationale(v2);
                        } else {
                            v2_1 = false;
                        }
                        if (v2_1) {
                            ++v3;
                            continue;
                        }

                        if (activity == null) {
                            break;
                        }

                        if (arg10 == null) {
                            break;
                        }

                        if (arg10.length > 0) {
                            v4_1 = activity.getResources().getString(com.ss.android.article.browser.R.string.REQUEST_PERMISSION_DENIED_DESCRIPTION);
                            if (activity != null && arg10 != null && arg10.length > 0) {
                                v5 = new StringBuilder();
                                int v6 = arg10.length;
                                v2_2 = 0;
                                v3 = 0;
                                while (v2_2 < v6) {
                                    v0_2 = arg10[v2_2];
                                    if (!getInstance().a(((Context) activity), v0_2)) {
                                        v0_3 = ((Integer) h.get(v0_2)).intValue();
                                        if (v0_3 > 0) {
                                            if (v3 == arg10.length - 1) {
                                                v5.append(activity.getString(v0_3));
                                            } else {
                                                v5.append(activity.getString(v0_3)).append("；");
                                            }
                                        }
                                    }

                                    ++v3;
                                    ++v2_2;
                                }

                                v0_2 = v5.toString();
                            }
                            if (TextUtils.isEmpty(v0_2)) {

                            } else {
                                v2 = String.format(v4_1, v0_2);
                                e v3_1 = new e(activity);
                                AlertDialog.Builder v0_4 = b != null ? com.ss.android.j.b.createDialogBuilder(((Context) activity)) : new AlertDialog.Builder(((Context) activity));
                                AlertDialog v0_5 = v0_4.setMessage(((CharSequence) v2)).setPositiveButton(com.ss.android.article.browser.R.string.confirm, ((DialogInterface.OnClickListener) v3_1)).setNegativeButton(com.ss.android.article.browser.R.string.cancel, null).create();
                                ((Dialog) v0_5).setCanceledOnTouchOutside(false);
                                ((Dialog) v0_5).show();
                            }
                        }
                        break;
                    }
                    ++v3;
                    continue;
                }
            } catch (Throwable v0_1) {
                v0_1.printStackTrace();
            }
        }

        v0_3 = arg10.length;
        if (arg11.length < v0_3) {
            v3 = arg11.length;
        } else {
            v3 = v0_3;
        }

        v4_2 = this.e.iterator();
        while (true) {
            if (!v4_2.hasNext()) {
                Iterator v0_7 = this.f.iterator();
                while (v0_7.hasNext()) {
                    v0_7.next();
                    v0_7.remove();
                }

                for (v0_3 = 0; true; ++v0_3) {
                    if (v0_3 >= v3) {
                        return;
                    }
                    this.mRequestingPermissions.remove(arg10[v0_3]);
                }
            }

            Object v0_6 = v4_2.next().get();
            v2_2 = 0;
            while (true) {
                if (v2_2 >= v3) {
                    break;
                }

                if (v0_6 != null && !((PermissionsResultAction) v0_6).onResult(arg10[v2_2], arg11[v2_2])) {
                    ++v2_2;
                    continue;
                }
                v4_2.remove();
                break;
            }
        }
    }

    public synchronized final void requestPermission(@Nullable Activity activity, @NonNull String[] permissions, @Nullable PermissionsResultAction permissionsResultAction) {
        int v4 = 23;
        if (activity != null) {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
                this.dispatchPermissionResult23(activity, permissions, permissionsResultAction);
                return;
            }

            this.a(permissions, permissionsResultAction);
            List v2 = this.c(activity, permissions, permissionsResultAction);
            if (v2.isEmpty()) {
                this.a(permissionsResultAction);
                return;
            }

            Object[] v1_1 = v2.toArray(new String[v2.size()]);
            this.mRequestingPermissions.addAll(v2);
            if (Build.VERSION.SDK_INT >= v4) {
                if ((activity instanceof RequestPermissionsRequestCodeValidator)) {
                    ((RequestPermissionsRequestCodeValidator) activity).validateRequestPermissionsRequestCode(1);
                }

                activity.requestPermissions(((String[]) v1_1), 1);
                return;
            }

            if (!(activity instanceof OnRequestPermissionsResultCallback)) {
                return;
            }
            new Handler(Looper.getMainLooper()).post(new com.ss.android.common.app.permission.b(((String[]) v1_1), activity));
        }
    }

    public synchronized final boolean a(@Nullable Context context, @NonNull String permission) {
        if (context != null) {
            if (PermissionUtils.checkPermission(context, permission) != 0 && (this.d.contains(permission))) {
                return false;
            }
            return true;
        } else {
            return false;
        }
    }

    public synchronized final void requestPermissions(@NonNull Fragment fragment,
                                                      @NonNull String[] permissions,
                                                      @Nullable PermissionsResultAction permissionsResultAction) {
        FragmentActivity activity = fragment.getActivity();
        if (activity != null) {
            if (Build.VERSION.SDK_INT < 23) {
                this.dispatchPermissionResult23(activity, permissions, permissionsResultAction);
                return;
            }

            this.a(permissions, permissionsResultAction);
            List v1 = this.c(activity, permissions, permissionsResultAction);
            if (v1.isEmpty()) {
                this.a(permissionsResultAction);
                return;
            }

            Object[] v0_2 = v1.toArray(new String[v1.size()]);
            this.mRequestingPermissions.addAll(((Collection) v1));
            fragment.requestPermissions(((String[]) v0_2), 1);
        }
    }

    public final synchronized boolean a(@Nullable Context context, @NonNull String[] strArr) {
        boolean z = false;
        if (context != null) {
            z = true;
            int i = 0;
            while (i < 2) {
                boolean a2 = a(context, strArr[i]) & z;
                i++;
                z = a2;
            }
        }
        return z;
    }


    private synchronized void a(@Nullable PermissionsResultAction arg4) {
        Iterator v1 = this.e.iterator();
        while (v1.hasNext()) {
            Object v0_1 = v1.next();
            if (((WeakReference) v0_1).get() != arg4 && ((WeakReference) v0_1).get() != null) {
                continue;
            }

            v1.remove();
        }

        v1 = this.f.iterator();
        while (v1.hasNext()) {
            if (v1.next() != arg4) {
                continue;
            }

            v1.remove();
        }
    }

    private synchronized void a(@NonNull String[] permissions, @Nullable PermissionsResultAction permissionsResultAction) {
        if (permissionsResultAction != null) {
            permissionsResultAction.registerPermissions(permissions);
            this.f.add(permissionsResultAction);
            this.e.add(new WeakReference(permissionsResultAction));
        }
    }

    private synchronized void b() {
        String str;
        for (Field field : Manifest.permission.class.getFields()) {
            try {
                str = (String) field.get("");
            } catch (IllegalAccessException e2) {
                Log.e(TAG, "Could not access field", e2);
                str = null;
            }
            this.d.add(str);
        }
    }


    private void dispatchPermissionResult23(@NonNull Activity activity, @NonNull String[] permissions, @Nullable PermissionsResultAction permissionsResultAction) {
        boolean result;
        int length = permissions.length;
        for (int i = 0; true; ++i) {
            if (i < length) {
                String permission = permissions[i];
                if (permissionsResultAction != null) {
                    try {
                        if (!this.d.contains(permission)) {
                            result = permissionsResultAction.onResult(permission, Permissions.NOT_FOUND);
                        } else if (PermissionUtils.checkPermission(activity, permission) != 0) {
                            result = permissionsResultAction.onResult(permission, Permissions.DENIED);
                        } else {
                            result = permissionsResultAction.onResult(permission, Permissions.GRANTED);
                        }
                        if (!result) {
                            continue;
                        }
                        return;
                    } catch (Throwable throwable) {
                        throwable.printStackTrace();
                        continue;
                    }
                } else {
                    continue;
                }
            }
            return;
        }
    }

    @NonNull
    private List c(@NonNull Activity activity, @NonNull String[] permissions, @Nullable PermissionsResultAction permissionsResultAction) {
        ArrayList<String> arrayList = new ArrayList(permissions.length);
        int length = permissions.length;
        for (int i = 0; i < length; ++i) {
            String permission = permissions[i];
            if (!this.d.contains(permission)) {
                if (permissionsResultAction != null) {
                    permissionsResultAction.onResult(permission, Permissions.NOT_FOUND);
                }
            } else if (PermissionUtils.checkPermission(activity, permission) != 0) {
                if (!this.mRequestingPermissions.contains(permission)) {
                    arrayList.add(permission);
                }
            } else if (permissionsResultAction != null) {
                permissionsResultAction.onResult(permission, Permissions.GRANTED);
            }
        }
        return arrayList;
    }

    public static final class a {
        public a() {

        }
    }
}

